package com.himiotech.core.framework.config.shiro.service;

import cn.hutool.core.util.StrUtil;
import com.himiotech.core.framework.config.async.AsyncFactory;
import com.himiotech.core.framework.config.async.AsyncManager;
import com.himiotech.core.framework.helper.constant.ShiroConstants;
import com.himiotech.core.framework.helper.constant.UserConstants;
import com.himiotech.core.framework.helper.exception.user.*;
import com.himiotech.core.framework.helper.utils.ServletUtils;
import com.himiotech.core.framework.helper.utils.ShiroUtils;
import com.himiotech.core.project.helper.enums.UserStatus;
import com.himiotech.core.project.model.entity.user.User;
import com.himiotech.core.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 登录校验方法
 *
 * @author ruoyi
 * @author wuwenbin
 */
@Service
public class LoginService {

    @Autowired
    private PasswordService passwordService;

    @Autowired
    private IUserService userService;

    /**
     * 登录
     */
    public User login(String username, String password) {
        // 验证码校验
        if (ShiroConstants.CAPTCHA_ERROR.equals(ServletUtils.getRequest().getAttribute(ShiroConstants.CURRENT_CAPTCHA))) {
            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, UserConstants.LOGIN_FAIL, "验证码错误"));
            throw new CaptchaException();
        }
        // 用户名或密码为空 错误
        if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)) {
            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, UserConstants.LOGIN_FAIL, "带 * 必须填写"));
            throw new UserNotExistsException();
        }
        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, UserConstants.LOGIN_FAIL, "用户不存在/密码错误"));
            throw new UserPasswordNotMatchException();
        }

        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, UserConstants.LOGIN_FAIL, "用户不存在/密码错误"));
            throw new UserPasswordNotMatchException();
        }

        // 查询用户信息
        User user = userService.selectUserByLoginName(username);

        if (user == null) {
            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, UserConstants.LOGIN_FAIL, "用户不存在/密码错误"));
            throw new UserNotExistsException();
        }

        if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, UserConstants.LOGIN_FAIL, "对不起，您的账号已被删除"));
            throw new UserDeleteException();
        }

        if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, UserConstants.LOGIN_FAIL, "用户已封禁，请联系管理员"));
            throw new UserBlockedException();
        }

        passwordService.validate(user, password);

        AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, UserConstants.LOGIN_SUCCESS, "登录成功"));
        recordLoginInfo(user);
        return user;
    }


    /**
     * 记录登录信息
     */
    public void recordLoginInfo(User user) {
        user.setLoginIp(ShiroUtils.getIp());
        user.setLoginDate(new Date());
        userService.updateById(user);
    }
}
